//
//  BLInfoTestController.m
//  Byelone
//
//  Created by 乔小乔 on 2021/1/24.
//

#import "BLInfoTestController.h"
#import "BLInfoTestCell.h"
#import "BLInfoTestTitleCell.h"
#import "BLInfoTestResultVC.h"
#import "BLInfoModel.h"

static NSString *reuse_BLInfoTestCell = @"Reuse_BLInfoTestCell";
static NSString *reuse_BLInfoTestTitleCell = @"Reuse_BLInfoTestTitleCell";

@interface BLInfoTestController ()<UITableViewDelegate,UITableViewDataSource>
@property (unsafe_unretained, nonatomic) IBOutlet UIView *viewTip;
@property (unsafe_unretained, nonatomic) IBOutlet UIImageView *imgBj;
@property (unsafe_unretained, nonatomic) IBOutlet UILabel *labelContent;
@property (unsafe_unretained, nonatomic) IBOutlet UITableView *tableViewList;
@property (strong, nonatomic) IBOutlet UIView *viewHeader;
@property (strong, nonatomic) IBOutlet UIView *viewFooter;
@property (unsafe_unretained, nonatomic) IBOutlet UIButton *btnSubmit;
@property (weak, nonatomic) IBOutlet UIButton *btnLast;
@property (weak, nonatomic) IBOutlet UIButton *btnNext;

@property (nonatomic, strong) NSMutableArray *arrayData;
@property (nonatomic, strong) NSMutableArray *arraySelect;
@property (nonatomic, strong) BLInfoTestModel *testModel;
@property (nonatomic, assign) NSInteger currentIndex;
@end

@implementation BLInfoTestController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.arrayData = [NSMutableArray new];
    self.arraySelect = [NSMutableArray new];
    self.currentIndex = 0;
    
    if (self.testType == BLInfoTestTypeTrait) {
        self.navigationItem.title = @"性格测试";
        self.labelContent.text = @"本次性格测试主要用于推荐相应对象时达到用户\n预期的精精准度，共20道题，赶快开始吧！";
        self.imgBj.image = [UIImage imageNamed:@"info_test_trait"];
    }else{
        self.navigationItem.title = @"期望对象";
        self.labelContent.text = @"本次期望对象征集主要用于推荐您预期的对象，\n共10道题，赶快开始吧！";
        self.imgBj.image = [UIImage imageNamed:@"info_test_lover"];
    }
    [self createUI];
    
    [self loadData];
}
- (void)createUI{
    [self.tableViewList registerNib:[UINib nibWithNibName:@"BLInfoTestCell" bundle:nil] forCellReuseIdentifier:reuse_BLInfoTestCell];
    [self.tableViewList registerNib:[UINib nibWithNibName:@"BLInfoTestTitleCell" bundle:nil] forCellReuseIdentifier:reuse_BLInfoTestTitleCell];
    
    self.tableViewList.tableHeaderView = self.viewHeader;
    self.tableViewList.tableFooterView = self.viewFooter;
    
    self.btnLast.layer.borderWidth = 1;
    self.btnLast.layer.borderColor = [UIColor chColorWithHexString:@"#6C72F5"].CGColor;
    self.btnNext.layer.borderWidth = 1;
    self.btnNext.layer.borderColor = [UIColor chColorWithHexString:@"#6C72F5"].CGColor;
}
- (void)refreshData{
    
}
- (IBAction)clickTest:(id)sender {
    self.viewTip.hidden = YES;
    self.tableViewList.hidden = NO;
}

- (IBAction)clickLast:(UIButton *)sender {
    if (self.currentIndex==0) {
        [HUD showtip:@"已经是第一道题"];
        return;
    }
    self.currentIndex--;
    [self.tableViewList reloadData];
}
- (IBAction)clickNext:(UIButton *)sender {
    if (self.currentIndex==self.arrayData.count-1) {
        [HUD showtip:@"已经是最后一道题"];
        return;
    }
    self.currentIndex++;
    [self.tableViewList reloadData];
}
- (IBAction)clickSubmit:(id)sender {
    for (BLTestSelectInfo *info in self.arraySelect) {
        if (info.optionId.length<=0) {
            [HUD showtip:@"您的题目未答全"];
            return;
        }
    }
    
    BLInfoTestModel *model = [[BLInfoTestModel alloc]init];
    model.testType = self.testType;
    model.testResultList = self.arraySelect;
    NSDictionary *dict = [model mj_keyValues];
    
    [BLHttpTool postJson:BL_TEST_SUBMITLIST params:dict successBlock:^(NSDictionary * _Nonnull obj) {
        if ([obj[@"content"] isKindOfClass:[NSDictionary class]]) {
            BLTestResultModel *testModel = [BLTestResultModel mj_objectWithKeyValues:obj[@"content"]];
            if (testModel.testResultItemList.count>0) {
                BLInfoTestResultVC *vc = [[BLInfoTestResultVC alloc]init];
                vc.testType = (BLTestResultType)self.testType;
                vc.isFromTest = YES;
                vc.arrayResult = testModel.testResultItemList;
                [self.navigationController pushViewController:vc animated:YES];
            }
        }
    } failedBlock:^(NSError * _Nonnull error) {
    }];
}

- (void)loadData{
    NSDictionary *dict = @{@"testType":@(self.testType)};
    [BLHttpTool getHttpRequest:BL_TEST_STEMLIST param:dict finish:^(NSURLSessionDataTask * _Nonnull data, NSDictionary * _Nonnull obj) {
        if (obj && [obj isKindOfClass:[NSDictionary class]]) {
            NSMutableDictionary *dict = [[NSMutableDictionary alloc]initWithDictionary:obj];
            dict[@"testResultList"] = dict[@"content"];
            self.testModel = [BLInfoTestModel mj_objectWithKeyValues:dict];
            [self.arrayData removeAllObjects];
            [self.arraySelect removeAllObjects];
            
            [self.arrayData addObjectsFromArray:self.testModel.content];
            [self.arraySelect addObjectsFromArray:self.testModel.testResultList];
            [self.tableViewList reloadData];
        }
    } failedBlock:^(id  _Nonnull data, NSError * _Nonnull error) {
        
    }];
}

#pragma mark - delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    if (self.arrayData.count>0) {
        return 1;
    }
    return 0;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    BLTestContentInfo *info = self.arrayData[self.currentIndex];
    return info.optionList.count+1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    BLTestContentInfo *info = self.arrayData[self.currentIndex];
    if (indexPath.row==0) {
        BLInfoTestTitleCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse_BLInfoTestTitleCell forIndexPath:indexPath];
        
        NSString *strPage = [NSString stringWithFormat:@"%ld",self.currentIndex+1];
        NSString *strTitle = [NSString stringWithFormat:@"(%@/%ld)%@",strPage,self.arrayData.count,info.stemName];
        NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc]initWithString:strTitle];
        [attStr addAttributes:@{NSForegroundColorAttributeName:[UIColor chColorWithHexString:@"#6C72F5"]} range:NSMakeRange(1, strPage.length)];
        cell.labelTitle.attributedText = attStr;
        return cell;
    }
    BLTestOptionItem *item = info.optionList[indexPath.row-1];
    BLInfoTestCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse_BLInfoTestCell forIndexPath:indexPath];
    [cell.btnTitle setTitle:[NSString stringWithFormat:@"%@ %@",item.optionNo,item.optionName] forState:UIControlStateNormal];
    if ([info.selectNo isEqualToString:item.optionNo]) {
        cell.btnTitle.backgroundColor = [UIColor chColorWithHexString:@"#6C72F5"];
        cell.btnTitle.selected = YES;
    }else{
        cell.btnTitle.backgroundColor = [UIColor chColorWithHexString:@"#F0F1FF"];
        cell.btnTitle.selected = NO;
    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
//    if (indexPath.row == 0) {
        return UITableViewAutomaticDimension;
//    }
//    return 37;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        return;
    }else{
        BLTestContentInfo *info = self.arrayData[self.currentIndex];
        BLTestOptionItem *item = info.optionList[indexPath.row-1];
        BLTestSelectInfo *selectInfo = self.arraySelect[self.currentIndex];
        if (![info.selectNo isEqualToString:item.optionNo]) {
            info.selectNo = item.optionNo;
            selectInfo.optionId = item.optionId;//存储已选择id
            [self.tableViewList reloadData];
        }
    }
}
@end
